package com.erp.server.DB;

import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;

public class DbConn {
	private static DataSource ds = null;
	private static Connection conn = null;

	public static Connection getConn_jdbc() {
		try {
			String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=apollo";
			String username = "sa";
			String password = "jlthldy2009sql2005";
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
			conn = DriverManager.getConnection(url, username, password);
			return conn;
		} catch (Exception e) {
			System.err.println("数据库连接异常:" + e.getMessage());
			return null;
		}
	}

	public static Connection getCon() {
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDatasource");

			conn = ds.getConnection();
			return conn;
		} catch (Exception e) {
			System.err.println("数据库连接异常: " + e.getMessage());
			return null;
		}
	}

	public void CloseConn() {
		try {
			conn.close();
		} catch (SQLException e) {
			System.err.println("数据库连接关闭异常: " + e.getMessage());
		}
	}

    /*public static void main(String[] a) {
    Connection conn;
    DbConn dc = new DbConn();
    conn = dc.getConn_jdbc();
    try {
    Statement stmt = conn.createStatement();
    String strSql = "select * from userBaseInf";
    ResultSet rs = stmt.executeQuery(strSql);
    while (rs.next()) {
    System.out.println("���⣺" + rs.getString("title"));
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    dc.CloseConn();
    }
    }*/
}
